﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenTK;
using DDD.Core;
using DDD.OpenTK;
using FloatComparer = DDDTest.Helper.FloatComparer;

namespace DDDTest.OpenTK.TestAttribute {
    [TestClass]
    public class TestAttribute_int_ivec2_ivec3_ivec4_ {

        #region Field
        GameWindow window;
        World wld;
        Graphics3D g3d;
        FrameBuffer fbuf;
        #endregion


        #region Setup
        [TestInitialize ()]
        public void MyClassInitialize () {
            window = new GameWindow ();
            wld = CreateWorld ();
            fbuf = CreateFrameBuffer ();

            g3d = Graphics3D.GetInstance ();
            g3d.Reset ();

            g3d.RenderPass += delegate {
                g3d.SetFrameBuffer (fbuf);
                g3d.SetWorld (wld);
                g3d.SetViewort (0, 0, 1, 1);
            };
        }

        [TestCleanup ()]
        public void MyClassCleanup () {
            window.Dispose ();
            window = null;
            wld = null;
        }

        private static FrameBuffer CreateFrameBuffer () {
            var fbuf = new FrameBuffer (4, 0, 0);
            var color0 = new Image2D<int> (PixelFormat.R32i, 1, 1);
            var color1 = new Image2D<int> (PixelFormat.RG32i, 1, 1);
            var color2 = new Image2D<int> (PixelFormat.RGB32i, 1, 1);
            var color3 = new Image2D<int> (PixelFormat.RGBA32i, 1, 1);
            fbuf.SetColorBuffer (0, color0);
            fbuf.SetColorBuffer (1, color1);
            fbuf.SetColorBuffer (2, color2);
            fbuf.SetColorBuffer (3, color3);
            return fbuf;
        }


        private World CreateWorld () {
            var cam = new Camera ();
            cam.SetPerspective (45, 1, 0.1f, 10f);

            var camNode = new Node ();
            camNode.Attach (cam);
            camNode.SetTranslation (0, 0, 1);

            var pos = new float[3] { 0, 0, 0};
            var col0 = new int[1] { -1 };
            var col1 = new int[2] { -1, 0 };
            var col2 = new int[3] { -1, 0, 1 };
            var col3 = new int[4] { -1, 0, 1, 2 };
            var posArray = new VertexArray<float> (1, 3);
            var colArray0 = new VertexArray<int> (1, 1);
            var colArray1 = new VertexArray<int> (1, 2);
            var colArray2 = new VertexArray<int> (1, 3);
            var colArray3 = new VertexArray<int> (1, 4);
            posArray.Set (0, 1, pos);
            colArray0.Set (0, 1, col0);
            colArray1.Set (0, 1, col1);
            colArray2.Set (0, 1, col2);
            colArray3.Set (0, 1, col3);

            var vbuf = new VertexBuffer ();
            vbuf.AddVertexArray ("v_Position", posArray);
            vbuf.AddVertexArray ("v_Color0", colArray0);
            vbuf.AddVertexArray ("v_Color1", colArray1);
            vbuf.AddVertexArray ("v_Color2", colArray2);
            vbuf.AddVertexArray ("v_Color3", colArray3);

            var ibuf = new IndexBuffer<int> (PrimitiveType.Point, 1);
            var indices = new int[] { 0 };
            ibuf.Set (0, 1, indices);

            var vert = @"#version 400
                         in  vec3   v_Position;
                         in  int    v_Color0;
                         in  ivec2  v_Color1;
                         in  ivec3  v_Color2;
                         in  ivec4  v_Color3;
                         flat out int    f_Color0;
                         flat out ivec2  f_Color1;
                         flat out ivec3  f_Color2;
                         flat out ivec4  f_Color3;
                               
                         void main () {
                             gl_Position = vec4(v_Position, 1);
                             f_Color0 = v_Color0;
                             f_Color1 = v_Color1;
                             f_Color2 = v_Color2;
                             f_Color3 = v_Color3;
                         }
                        ";
            var frag = @"#version 400
                         flat in  int    f_Color0;
                         flat in  ivec2  f_Color1;
                         flat in  ivec3  f_Color2;
                         flat in  ivec4  f_Color3;
                         out int    frag_Color0;
                         out ivec2  frag_Color1;
                         out ivec3  frag_Color2;
                         out ivec4  frag_Color3;
                               
                         void main () {
                             frag_Color0 = f_Color0;
                             frag_Color1 = f_Color1;
                             frag_Color2 = f_Color2;
                             frag_Color3 = f_Color3;
                         }
                        ";

            var vs = new VertexShader (new MemoryStream (Encoding.UTF8.GetBytes (vert)));
            var fs = new FragmentShader (new MemoryStream (Encoding.UTF8.GetBytes (frag)));
            var sp = new ShaderProgram (vs, fs);

            var app = new Appearance ();
            app.SetShaderProgram (sp);

            var msh = new Mesh ();
            msh.SetVertexBuffer (vbuf);
            msh.SetIndexBuffer (ibuf);
            msh.SetAppearance (app);

            var mshNode = new Node ();
            mshNode.Attach (msh);

            var wld = new World ();
            wld.AddChild (camNode);
            wld.AddChild (mshNode);
            wld.SetActiveCamera (camNode);

            return wld;
        }

        #endregion



        #region TestMethod
        [TestMethod]
        public void TestMethod1 () {

            g3d.Render ();
            window.SwapBuffers ();

            var expected0 = new int[1] { -1};
            var expected1 = new int[2] { -1, 0 };
            var expected2 = new int[3] { -1, 0, 1 };
            var expected3 = new int[4] { -1, 0, 1, 2 };
            var pixels0 = new int[1];
            var pixels1 = new int[2];
            var pixels2 = new int[3];
            var pixels3 = new int[4];
            var color0 = fbuf.GetColorBuffer (0) as Image2D<int>;
            var color1 = fbuf.GetColorBuffer (1) as Image2D<int>;
            var color2 = fbuf.GetColorBuffer (2) as Image2D<int>;
            var color3 = fbuf.GetColorBuffer (3) as Image2D<int>;
            color0.Capture ();
            color1.Capture ();
            color2.Capture ();
            color3.Capture ();
            color0.Get (0, 0, 1, 1, pixels0);
            color1.Get (0, 0, 1, 1, pixels1);
            color2.Get (0, 0, 1, 1, pixels2);
            color3.Get (0, 0, 1, 1, pixels3);

            CollectionAssert.AreEqual (expected0, pixels0);
            CollectionAssert.AreEqual (expected1, pixels1);
            CollectionAssert.AreEqual (expected2, pixels2);
            CollectionAssert.AreEqual (expected3, pixels3);
        }
        #endregion



    }
}
